package Dao.Impl;

import Dao.CarDAO;
import MyUtil.JDBCUtils;
import pojo.Car;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CarDAOImpl implements CarDAO {

    public CarDAOImpl() throws Exception {
    }

    //查询所有车辆展示，分别被用户端和管理员端调用
    @Override
    public List<Car> getAllCars() throws Exception {
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        //车辆集合
        List<Car> carList = new ArrayList<>();

        //查询sql
        sql = "select * from car";
        ps = carDAoImplConn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()){
            //封装数据
            Car car = new Car();
            car.setCar_id(rs.getInt("car_id"));
            car.setModel(rs.getString("model"));
            car.setBrand(rs.getString("brand"));
            car.setLicense_plate(rs.getString("license_plate"));
            car.setStatus(rs.getString("status"));
            car.setRental_rate(rs.getInt("rental_rate"));
            car.setFuel_type(rs.getString("fuel_type"));
            car.setColor(rs.getString("color"));
            car.setMileage(rs.getInt("mileage"));
            //加入集合
            carList.add(car);
        }
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,rs);

        return carList;
    }

    //carBrand查询车辆展示，分别被用户端和管理员端调用
    @Override
    public List<Car> getAllCarsByBrand(String carBrand) throws Exception {
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        //车辆集合
        List<Car> carList = new ArrayList<>();

        //查询sql
        sql = "select * from car where brand LIKE ?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1,"%"+carBrand+"%");
        rs = ps.executeQuery();
        while (rs.next()){
            //封装数据
            Car car = new Car();
            car.setCar_id(rs.getInt("car_id"));
            car.setModel(rs.getString("model"));
            car.setBrand(rs.getString("brand"));
            car.setLicense_plate(rs.getString("license_plate"));
            car.setStatus(rs.getString("status"));
            car.setRental_rate(rs.getInt("rental_rate"));
            car.setFuel_type(rs.getString("fuel_type"));
            car.setColor(rs.getString("color"));
            car.setMileage(rs.getInt("mileage"));
            //加入集合
            carList.add(car);
        }
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,rs);

        return carList;
    }

    //用户在用户界面租车更新车的信息
    @Override
    public Boolean updateByCar(Car car) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        //查询sql
        sql = "UPDATE car set status=? where car_id=?";
        //执行sql
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1,car.getStatus());
        ps.setInt(2,car.getCar_id());
        int count = ps.executeUpdate();
        while (count>0){
            return true;
        }
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,null);
        return false;
    }

    //用户在用户界面还车时进行更新信息
    @Override
    public Boolean updateCarByOrder(int carId, String startCarStatus, String changeCarStatus) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        //更新sql
        sql = "UPDATE car set status=? where car_id=? and status=?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1,changeCarStatus);
        ps.setInt(2,carId);
        ps.setString(3,startCarStatus);
        int count = ps.executeUpdate();
        while (count>0){
            return true;
        }
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,null);

        return false;
    }

    //根据车辆id查询车辆
    @Override
    public Car getCarById(int carId) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        //查询sql
        sql = "select * from car where car_id=?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setInt(1,carId);
        rs = ps.executeQuery();
        Car car = new Car();
        car.setCar_id(carId);
        while (rs.next()){
            car.setCar_id(rs.getInt("car_id"));
            car.setModel(rs.getString("model"));
            car.setBrand(rs.getString("brand"));
            car.setLicense_plate(rs.getString("license_plate"));
            car.setStatus(rs.getString("status"));
            car.setRental_rate(rs.getInt("rental_rate"));
            car.setFuel_type(rs.getString("fuel_type"));
            car.setColor(rs.getString("color"));
            car.setMileage(rs.getInt("mileage"));
        }

        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,null);

        return car;
    }

    //管理员添加车辆信息
    @Override
    public Boolean addCar(Car car) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;
        sql = "insert into car(model, brand, license_plate,status, rental_rate, fuel_type, color, mileage) values (?, ?, ? , ?, ?, ?,  ?, ?)";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1, car.getModel()); // Assuming this is the correct model field.
        ps.setString(2, car.getBrand()); // If brand should be included in the SQL statement.
        ps.setString(3, car.getLicense_plate()); // If license plate should be included in the SQL statement.
        ps.setString(4, "可租"); // If status should be included in the SQL statement.
        ps.setInt(5, car.getRental_rate()); // Assuming this is the correct field for rental rate.
        ps.setString(6, car.getFuel_type()); // If fuel type should be included in the SQL statement.
        ps.setString(7, car.getColor()); // If color should be included in the SQL statement.
        ps.setInt(8, car.getMileage()); // Assuming this is the correct field for mileage.
        int count = ps.executeUpdate();
        if (count != 0) {
            return true;
        }
        return false;
    }

    //管理员删除车辆信息
    @Override
    public void deleteCar(int carId) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        sql = "DELETE FROM car WHERE car_id = ?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1, String.valueOf(carId));
        ps.executeUpdate();
    }

    //管理员取消订单车辆更新状态
    @Override
    public void updateCarStatus(int carId) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        //更新sql
        sql = "UPDATE car set status=? where car_id=?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1,"可租");
        ps.setInt(2,carId);
        int count = ps.executeUpdate();
        System.out.println("取消订单车辆更新状态："+count);
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,null);
    }

    @Override
    public void updateCarById(int data_id, Car car) throws Exception {
        //获取连接
        Connection carDAoImplConn = JDBCUtils.getMyConnection();
        String sql;
        PreparedStatement ps;
        ResultSet rs;

        sql = "update  car set model = ?, brand = ?, license_plate = ?,status = ?, rental_rate = ?, fuel_type = ?, color = ?, mileage = ? where car_id = ?";

        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1, car.getModel()); // Assuming this is the correct model field.
        ps.setString(2, car.getBrand()); // If brand should be included in the SQL statement.
        ps.setString(3, car.getLicense_plate()); // If license plate should be included in the SQL statement.
        ps.setString(4, car.getStatus()); // If status should be included in the SQL statement.
        ps.setInt(5, car.getRental_rate()); // Assuming this is the correct field for rental rate.
        ps.setString(6, car.getFuel_type()); // If fuel type should be included in the SQL statement.
        ps.setString(7, car.getColor()); // If color should be included in the SQL statement.
        ps.setInt(8, car.getMileage());
        ps.setInt(9, data_id);
        ps.executeUpdate();
        //释放资源
        JDBCUtils.release(carDAoImplConn,ps,null);
    }
}
